package com.iskyonline.skyonlieapp.controller;

import com.iskyonline.skyonlieapp.dto.ResponseDTO;
import com.iskyonline.skyonlieapp.entity.KeyValue;
import com.iskyonline.skyonlieapp.exception.BizException;
import com.iskyonline.skyonlieapp.service.IndexService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public abstract class BaseController {
    @Value("${file.image.path}")
    protected String filePath;

    @Value("${file.image.url}")
    protected String fileUrl;

    protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    protected String getUserId(HttpSession session) {
        return "1000";
    }

    protected int getBoUserId(HttpSession session) {
        return 1000;
    }

    @ExceptionHandler(BizException.class)
    public ResponseDTO processBizException(BizException bie) {
        logger.error(bie.getMessage(), bie);
        return ResponseDTO.failed(bie.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseDTO processException(Exception e) {
        logger.error(e.getMessage(), e);
        return ResponseDTO.failed("系统异常，请查联系程序猿~");
    }

    protected void requstSave(HttpServletRequest request, IndexService indexService) {
        KeyValue keyValue = indexService.loadKeyValueByType(1);
        request.setAttribute("bannerPic", fileUrl + keyValue.getPicUrl());
        request.setAttribute("homeBanner", indexService.listKeyValue(2));
        request.setAttribute("hezuohuoban", indexService.listKeyValue(3));
        request.setAttribute("guanyuwomen", indexService.loadKeyValueByType(4).getValueText());
        request.setAttribute("waibaofuwu", indexService.loadKeyValueByType(5).getValueText());
        request.setAttribute("zhaoxiannashi", indexService.loadKeyValueByType(7).getValueText());
        request.setAttribute("lianxiwomen", indexService.loadKeyValueByType(8).getValueText());
        KeyValue keyValue1 = indexService.loadKeyValueByType(9);
        keyValue1.setPicUrl(fileUrl + keyValue1.getPicUrl());
        request.setAttribute("webInfo", keyValue1);
    }


}
